<?php
class CP_ContactsController extends XPHP_Controller
{
	/**
	 * Phương thức sẽ thực thi trước khi thực thi sau khi controller được khởi tạo
	 */
	public function _init()
	{
		//Load layout
		$this->loadLayout("Administrator");
		//Xác thực
		$this->helper->auth();
	}
	
	public function indexAction()
	{
		//Lấy ra toàn bộ danh sách Contacts
		$contacts = new Areas_CP_Models_Contacts();
		$contactsList = $contacts->select_all(false, "date_create DESC");
		
		//Trả về View
		$this->view->contactList = $contactsList;
		
		return $this->view();
	}
	
	public function editAction($args)
	{
		$model = new Areas_CP_Models_Contacts($args[0]);
		
		return $this->view($model);
	}
	
	/**
	 * [[
	 * model: Areas_CP_Models_Contacts
	 * ]]
	 */
	public function editPOST()
	{
		if($this->hasModel())
		{
			if($this->model->isValid())
			{
				$modelUpdate = new Areas_CP_Models_Contacts($this->model->id);
				$modelUpdate->content = $this->model->content;
				$modelUpdate->update();
				//Redirect về trang chủ
				return $this->redirect("index");
			}
		}
	}
	
	public function readAJAX()
	{
		$id = $this->params["id"];
		$status = $this->params["read"];
		//Sửa thông tin Contacts
		$contacts = new Areas_CP_Models_Contacts($id);
		$contacts->read = $status;
		$contacts->update();
		
		return $this->json(array("success" => true));
	}
	
	public function deleteAJAX()
	{
		$id = $this->params["id"];
		$Contacts = new Areas_CP_Models_Contacts($id);
		$Contacts->delete();
		return $this->json(array("success" => true));
	}
}